<?php

/**
 * BH造型单表model
 * @author perry
 */

namespace Home\Model;

use Think\Model;

class BHLooksModel extends Model {

	private $imgUploadType='';
    /**
     * 初始化
     */
    protected function _initialize() {
        $this->tableName = 'bh_looks';
        $this->trueTableName  = 'eosi_bh_looks';
        $this->imgUploadType = 'looks';
        $this->closetOrCommM=D('Home/BHCloset');
    }

    function getByPage($uid,$type=1,$status=1,$name=false,$page=1,$pageSize=10,$field=true,$id=-1,$order="show_order desc,update_time desc"){
    	$imgsubType=$this->imgUploadType;
    	$map=array();
    	if(ic_isPosInt($uid)){
    		//这是获得前台用户的衣橱数据
	    	$map['uid']=$uid;
	    }
	    elseif (is_array($uid)) {
	    	//这是获得管理员添加的百家好推荐造型
	    	// $super_style_uid_str=implode(",", $uid);
	    	// $map['_string'] = " uid in ($super_style_uid_str) ";
	    	$map['cate']=2;
	    }

	    if(ic_isPosInt($type)){
	    	if($type!=1){ //type==1 代表全部类型，无需筛选
		    	$map['label']=$type;
		    }
	    }
	    if(ic_isPosInt($status)){
		    	$map['status']=$status;
	    }
	    if($name!==false){
	    	$namef=ic_filterStr($name);
	    		if(ic_checkUnEmpty($namef)){
			    	// $map['name']=array('exp'," LIKE (1,3,8) ");
		    		$map['name'] = array('like',"%$name%");
		    	}
	    }
	    if(!ic_isPosInt($page)){
	    	$page=1;
	    }
	    if(!ic_isPosInt($pageSize)){
	    	$pageSize=10;
	    }
	    if($pageSize>50000){
	    	$pageSize=50000;
	    }
	    if(ic_isPosInt($id)){
	    	$map['id']=$id;
	    }
	    elseif (is_array($id)) {
	    	$map['id'] = array("in", $id);
	    }
	    elseif ($id!==-1) {
	    	$real_id_arr=$this->filterIdStr($id,true);
	    	if(count($real_id_arr)>0){
	    		$map['id'] = array("in", $real_id_arr);
	    	}
	    }
	    $totalCount=$this->where($map)->count();
	    // $debug_sql=$this->getLastSql();
	    $datas=array();
	    if(($totalCount>($page-1)*$pageSize)){
	    	$start= ($page-1)*$pageSize;
	    	if($field!==true){
	    		$field.=",create_time";
	    	}
	    	$datas=$this->where($map)->order($order)->field($field)->limit($start,$pageSize)->select();
	    	foreach ($datas as $key => &$value) {
	    		//如果返回数据里有主图，需要转换成图片全路径
	    		if(array_key_exists('img', $value)){
		    		$value['img']=ic_getUploadImgFullPath($imgsubType,$value['img'],$value['create_time']);
		    	}
		    	//如果返回数据里有item_ids，需要获得造型构成元素的全路径
		    	if(array_key_exists('item_ids', $value)){
		    		if(ic_checkUnEmpty($value['item_ids'])){
		    			//根据item_ids字段包含的一批主键获得造型构成元素（商品或衣橱）的详细信息
		    			//getByPage($uid,$type=1,$status=1,$name=false,$page=1,$pageSize=10,$field=true,$id=-1){
		    			$item_id_arr=explode(',',$value['item_ids']);
			    		$tmparr=$this->closetOrCommM->getByPage('notsure',-1,1,1,false,1,100,'id,uid,type,img,name,memo,sub_imgs,no,price,dh_date',$item_id_arr);
			    		$value['items'] = $tmparr['pageList'];
			    	}
			    	else {
			    		$value['items'] = array();
			    	}
		    	}
		    	//如果返回数据里有sub_imgs，需要获得造型幅图全路径
		    	if(array_key_exists('sub_imgs', $value)){
		    		if(ic_checkUnEmpty($value['sub_imgs'])){
			    		$tmparr=explode(',',$value['sub_imgs']);
				        foreach ($tmparr as $key2 => $value2) {
				            $tmparr[$key2] = ic_getUploadImgFullPath($imgsubType,$value2,$value['create_time']);
				        }
			    		$value['sub_imgs'] = $tmparr;
			    	}
			    	else {
			    		$value['sub_imgs'] = array();
			    	}
		    	}
		    	if(array_key_exists('create_time', $value)){
		    		$value['create_time']=ic_dateToStr($value['create_time'],'Y.m.d');
		    	}

	    	}
	    }
	    // dump($this->getLastSql());
	    $totalPage=floor($totalCount/$pageSize)+((($totalCount%$pageSize)==0)?0:1);
	    return array('totalCount'=>$totalCount,'totalPage'=>$totalPage, 'pageList'=>$datas);
    }

    function deleteOne($uid,$id){
    	if(!ic_isPosInt($id)){
	    	throw new \Exception("id参数有误.", -73);
	    }
    	$map=array('id'=>$id);
    	$exist=$this->where($map)->find();
    	if(!$exist){
    		throw new \Exception("待删除的数据不存在哦,请刷新页面重试.", -78);
    	}
    	if($exist['uid']!=$uid){
    		throw new \Exception("您无权限删除他人的数据，请检查.", -81);
    	}
    	//先删数据
    	$deleteRe=$this->where($map)->delete();
    	//再删物理图片
    	$oldfilePath=ic_getUploadImgServerPath($this->imgUploadType,$exist['create_time']);
        $this->delMulti($oldfilePath,$exist['img']);
        //最后删除关联日历数据
        //2017-09-19 perry add 需把该造型对应的日历记录也删除
        M('bh_calendar')->where("look_id=".$id)->delete();
        return $deleteRe;
    }

    private function delMulti($serverPath,$delName){
        @unlink($serverPath.$delName);
        @unlink($serverPath."m_".$delName);
        @unlink($serverPath."s_".$delName);
    }

    //对传入的逗号拼接的id字符串进行过滤，不为int的去除，剩下的重新拼接成字符串或数组返回
    public function filterIdStr($idstr,$returnArr=false){
    	$id_arr=explode(",", $idstr);
    	$real_id_arr=array();
    	foreach ($id_arr as $key => $iid) {
    		if(ic_isPosInt($iid)){
    			$real_id_arr[]=$iid;
    		}
    	}
    	return $returnArr? $real_id_arr:implode(",", $real_id_arr);
    }
    
    
    

    

}
